package com.gs.cd.service.resource.controller;


import com.gs.cd.api.ApiResult;
import com.gs.cd.model.resource.SysResource;
import com.gs.cd.service.resource.service.SysResourceService;
import com.seven.mybatisx.controller.AbstractController;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源模块
 *
 * @author seven
 * @since 2020-12-22
 */
@RestController
@RequestMapping({"/resource/sysResource"})
public class SysResourceController extends AbstractController<SysResource, SysResourceService> {

    /**
     * 获取树
     *
     * @param projectId
     * @return
     */
    @GetMapping("list/tree/{projectId}")
    public ApiResult listTree(@PathVariable String projectId) {
        // TODO: 2020/12/23 验证projectId是否存在
        List<SysResource> sysResourceList = iService.listTreeByProjectId(projectId);
        return ApiResult.success(sysResourceList);
    }

    /**
     * 复制到其他项目
     *
     * @param sourceProductId 源 项目
     * @param targetProductId 目标 项目
     * @return
     */
    @PostMapping("copy")
    public ApiResult copy(@RequestParam String sourceProductId, @RequestParam String targetProductId) {
        // TODO: 2020/12/23 check productId 的有效性
        boolean b = iService.copy(sourceProductId, targetProductId);
        return b ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 获取资源相关关联
     *
     * @param id
     * @return
     */
    @GetMapping("relatedgroup/{id}")
    public ApiResult relatedgroup(@PathVariable String id) {
        Map relatedgroup = iService.relatedgroup(id);
        return ApiResult.success(relatedgroup);
    }

    /**
     * 拖拽排序
     *
     * @param ids 有序的id数组
     * @return
     */
    @PostMapping("/drag")
    public ApiResult drag(@RequestBody List<String> ids) {
        return iService.drag(ids) ? ApiResult.success() : ApiResult.error();
    }

}

